﻿using System;
using System.Net;
using System.Windows.Forms;
using DVDFilmoteka.Settings;
using System.Threading;
using System.Globalization;

namespace DVDFilmoteka.Forms
{
    public partial class Settings : LocalizableForm
    {
        public Settings()
        {
            InitializeComponent();
        }

        private void Settings_Load(object sender, EventArgs e)
        {
            SettingsManager manager = SettingsManager.Instance;

            checkBoxAutomaticVersionCheck.Checked = manager.Data.AutomaticUpdate;
            checkBoxViewResults.Checked = manager.Data.ViewResults;
            checkBoxUseProxy.Checked = manager.Data.UseProxy;
            checkBoxDontChangeGenres.Checked = manager.Data.DontChangeGenres;
            checkBoxDontChangeNames.Checked = manager.Data.DontChangeNames;
            checkBoxLoadPicturesOnLine.Checked = manager.Data.LoadPicturesOnline;
            checkBoxBeta.Checked = manager.Data.BetaInform;
            if (manager.Data.UseProxy)
            {
                checkBoxUseDefaultProxy.Checked = manager.Data.UseDefaultProxy;
                if (!manager.Data.UseDefaultProxy)
                {
                    textBox1.Text = manager.Data.ProxyIPAddress.ToString();
                    textBox2.Text = manager.Data.ProxyPort.ToString();
                } else
                {
                    textBox1.Text = "";
                    textBox2.Text = "";
                }
            } else
            {
                textBox1.Text = "";
                textBox2.Text = "";
                checkBoxUseDefaultProxy.Checked = true;
            }

            checkBox2_CheckStateChanged(sender, e);
            checkBox3_CheckStateChanged(sender, e);

            comboBoxLanguage.Items.Clear();
            comboBoxLanguage.Items.Add(new Tools.LanguageItem(CultureInfo.InstalledUICulture.Name, Properties.Messages.languages_default));
            //comboBoxLanguage.Items.Add(new Tools.LanguageItem("en-US", Properties.Messages.languages_english));
            //comboBoxLanguage.Items.Add(new Tools.LanguageItem("cs-CZ", Properties.Messages.languages_czech));

            comboBoxLanguage.SelectedItem = new Tools.LanguageItem(SettingsManager.Instance.Data.Language, "");

            Localize();
        }


        // Use proxy
        private void checkBox2_CheckStateChanged(object sender, EventArgs e)
        {
            checkBoxUseDefaultProxy.Enabled = checkBoxUseProxy.Checked;
        }

        // Use default proxy
        private void checkBox3_CheckStateChanged(object sender, EventArgs e)
        {
            labelProxy.Enabled = textBox2.Enabled = textBox1.Enabled = !checkBoxUseDefaultProxy.Checked;
        }

        // Cancel
        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        // Ulozit
        private void button1_Click(object sender, EventArgs e)
        {
            if (!validate())
            {
                String message = Properties.Messages.messages_wrongformat;
                String caption = Tools.AssemblyName;
                const MessageBoxButtons buttons = MessageBoxButtons.OK;
                MessageBox.Show(this, message, caption, buttons, MessageBoxIcon.Error);

                return;
            }

            SettingsManager manager = SettingsManager.Instance;

            manager.Data.AutomaticUpdate = checkBoxAutomaticVersionCheck.Checked;
            manager.Data.ViewResults = checkBoxViewResults.Checked;
            manager.Data.UseProxy = checkBoxUseProxy.Checked;
            if (checkBoxUseProxy.Checked)
            {
                manager.Data.UseDefaultProxy = checkBoxUseDefaultProxy.Checked;
            }

            if (checkBoxUseProxy.Checked && !checkBoxUseDefaultProxy.Checked)
            {                
                manager.Data.ProxyIPAddress = textBox1.Text;
                manager.Data.ProxyPort = Int32.Parse(textBox2.Text);
            }

            manager.Data.DontChangeGenres = checkBoxDontChangeGenres.Checked;
            manager.Data.DontChangeNames = checkBoxDontChangeNames.Checked;
            manager.Data.LoadPicturesOnline = checkBoxLoadPicturesOnLine.Checked;
            manager.Data.BetaInform = checkBoxBeta.Checked;
            manager.Data.Language = Thread.CurrentThread.CurrentUICulture.Name;

            manager.SaveToFile();
            
            Close();
        }

        /// <summary>
        /// Validace policek
        /// </summary>
        /// <returns></returns>
        private bool validate()
        {
            if (checkBoxUseProxy.Checked && !checkBoxUseDefaultProxy.Checked)
            {
                if (String.IsNullOrEmpty(textBox1.Text) || String.IsNullOrEmpty(textBox2.Text)) return false;

                IPAddress pom;
                if (!IPAddress.TryParse(textBox1.Text, out pom)) return false;

                Int32 pom2;
                if (!Int32.TryParse(textBox2.Text, out pom2)) return false;
            }
            return true;
        }

        private void checkBoxAutomaticVersionCheck_CheckedChanged(object sender, EventArgs e)
        {
            checkBoxBeta.Enabled = checkBoxAutomaticVersionCheck.Checked;
            if (!checkBoxAutomaticVersionCheck.Checked)
            {
                checkBoxBeta.Checked = false;
            }
        }

        private void comboBoxLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            Tools.LanguageItem lang = comboBoxLanguage.SelectedItem as Tools.LanguageItem;

            // Nastaveni jazyka
            if (lang == null || String.IsNullOrEmpty(lang.ShortCode)) // Pojistka, nemelo by se stat
            {
                Thread.CurrentThread.CurrentUICulture = CultureInfo.InstalledUICulture; // Jazyk OS                
            }
            else
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang.ShortCode); // Vybrany jazyk
            }

            // Zavola vsechny otevrna okna, aby si nastavily jazyk
            foreach (Form item in Application.OpenForms)
            {
                if (item is LocalizableForm)
                {
                    ((LocalizableForm)item).Localize();
                }
            }
        }

        public override void Localize()
        {
            this.Text = Properties.Messages.form_settings_text;
            groupBoxBehaviour.Text = Properties.Messages.form_settings_groupBoxBehaviour;
            groupBoxConnection.Text = Properties.Messages.form_settings_groupBoxConnection;
            groupBoxLanguage.Text = Properties.Messages.form_settings_groupBoxLanguage;
            groupBoxUpdates.Text = Properties.Messages.form_settings_groupBoxUpdates;
            checkBoxAutomaticVersionCheck.Text = Properties.Messages.form_settings_checkBoxAutomaticVersionCheck;
            checkBoxBeta.Text = Properties.Messages.form_settings_checkBoxBeta;
            checkBoxDontChangeGenres.Text = Properties.Messages.form_settings_checkBoxDontChangeGenres;
            checkBoxDontChangeNames.Text = Properties.Messages.form_settings_checkBoxDontChangeNames;
            checkBoxLoadPicturesOnLine.Text = Properties.Messages.form_settings_checkBoxLoadPicturesOnLine;
            checkBoxUseDefaultProxy.Text = Properties.Messages.form_settings_checkBoxUseDefaultProxy;
            checkBoxUseProxy.Text = Properties.Messages.form_settings_checkBoxUseProxy;
            checkBoxViewResults.Text = Properties.Messages.form_settings_checkBoxViewResults;
            labelProxy.Text = Properties.Messages.form_settings_labelProxy;
            buttonCancel.Text = Properties.Messages.form_settings_buttonCancel;
            buttonSave.Text = Properties.Messages.form_settings_buttonSave;
        }        
    }
}
